﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Windows.Infrastructure.EFCore.Share.Extensions;
using Windows.Infrastructure.Share.CustomException;
using Windows.Infrastructure.Share.Extensions;
using Windows.Service.Share;
using Windows.WorkFlow.Domain;
using Windows.WorkFlow.Infrastructure.EFCore;

namespace Windows.WorkFlow.Service
{
    public class FlowCategoryService : IFlowCategoryService
    {
        private readonly IMapper _mapper;
        //private readonly ICache _cache;
        private readonly WorkFlowDbContext _db;
        public FlowCategoryService(IMapper mapper, WorkFlowDbContext db)
        {
            _mapper = mapper;
            _db = db;
        }
        /// <summary>
        /// 获取所有流程类型基础资料
        /// </summary>
        /// <returns></returns>
        public async Task<List<FlowCategoryResponse>> GetBasicFlowCategorys()
        {
            var query =  _db.FlowCategory.AsQueryable();
            var list = await query.ToListAsync();
            return _mapper.Map<List<FlowCategoryResponse>>(list);
        }
        /// <summary>
        /// 流程类型视图
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PageResponse<FlowCategoryResponse>> Query(PageRequest<FlowCategoryRequest> request)
        {
            var query = Where(request);
            var list = await query.Paging(request.PageIndex, request.PageSize).ToListAsync();
            var response = new PageResponse<FlowCategoryResponse>
            {
                TotalCount = await query.CountAsync(),
                PageIndex = request.PageIndex,
                PageSize = request.PageSize,
                List = _mapper.Map<List<FlowCategoryResponse>>(list)
            };
            return response;
        }
        /// <summary>
        /// 获取id的流程类型详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<FlowCategoryResponse> Detail(int id)
        {
            var model = await _db.FlowCategory.FindByIdAsync(id);
            return _mapper.Map<FlowCategoryResponse>(model);
        }
        /// <summary>
        /// 流程类型保存
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        public async System.Threading.Tasks.Task Save(FlowCategorySaveRequest request)
        {
            if (await _db.FlowCategory.AnyAsync(x => x.Name.ToLower() == request.Name && x.Id != request.Id))
                throw new ApiException("流程类型名称已存在!");
            FlowCategory? model = _mapper.Map<FlowCategory>(request);
            if (request.Id == null)
            {
                await _db.FlowCategory.AddAsync(model);
            }
            else
            {
                model = await _db.FlowCategory.FindByIdAsync(request.Id.Value);
                if (model == null)
                    throw new ApiException("流程类型不存在!");
                model.Name = request.Name;
                model.Sort = request.Sort;
                model.Remark = request.Remark;
            }
            await _db.SaveChangesAsync();
        }
        /// <summary>
        /// 流程类型删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async System.Threading.Tasks.Task Remove(IdsRequest request)
        {
            //var names = await _db.PostLevel.Include(x => x.Posts).Where(x => request.Ids.Contains(x.Id) && x.Posts.Count > 0).Select(s => s.Name).ToListAsync();
            //if (names.Count > 0)
            //{
            //    throw new ApiException($"职级:{string.Join(',', names)}已被分配,不允许删除!");
            //}
            await _db.FlowCategory.RemoveAsync(request.Ids);
            await _db.SaveChangesAsync();
        }
        private IQueryable<FlowCategory> Where(PageRequest<FlowCategoryRequest> request)
        {
            IQueryable<FlowCategory> query = _db.FlowCategory.AsQueryable();
            if (!request.Query.Name.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.Name.Contains(request.Query.Name!));
            }
            return query;
        }
    }
}
